package com.yoomiito.app.ui.list

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.gift.GiftGoodsList
import com.yoomiito.app.model.gift.GiftType
import com.yoomiito.app.model.order.ShopCarList
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.utils.NetworkUtils

class GoodsSortList_1Presenter(appConfig: AppConfig) :BaseActPresenter<GoodsSortList_1Activity>(appConfig){
//    val model = GoodsSortListRepository()
    /**
     * 获取商品列表
     */
    fun getGoodsList(sort: Int, page: Int, type: Long, categorizeId: Int,salesSort: Int) {
//        model.getGoodsList(sort, page, type, goodsType)
        YmApi.getYmService()
                .getVipGiftGood(sort, page, type, categorizeId, salesSort, 10)
                .compose<BaseModel<GiftGoodsList>>(v.bindToLifecycle<BaseModel<GiftGoodsList>>())
                .compose<GiftGoodsList>(XApi.getApiTransformerData<GiftGoodsList>())
                .compose<GiftGoodsList>(XApi.getScheduler<GiftGoodsList>())
                .subscribe(object : ApiSubscriber<GiftGoodsList?>(handleException()) {
                    override fun onSuccess(giftGoodsList: GiftGoodsList?) {
                        v.showGift(giftGoodsList!!)
                        v.dismissLoading()
                    }

                    override fun onFail(error: ApiException) {
                        v.dismissLoading()
                    }
                })
    }

    /**
     * 获取商品类型列表
     */
    fun getGoodsType(id: Int) {
        if (NetworkUtils.checkNetPermission(v)) {
            return
        }
        YmApi.getYmService()
                .getGiftType(id)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData<List<GiftType>>())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<List<GiftType>>(handleException()) {
                    override fun onSuccess(giftTypes: List<GiftType>) {
                        if (hasV()){
                            v.setType(giftTypes.toMutableList())
                        }
                    }

                    override fun onFail(error: ApiException) {

                    }
                })
    }

    fun getList(page: Int) {
        YmApi.getYmService()
                .showCarList(page, 10)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData<ShopCarList>())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<ShopCarList>(handleException()) {
                    override fun onSuccess(shopCarList: ShopCarList?) {
                        if (hasV()){
                            v.showNewFlag(shopCarList != null && shopCarList?.data != null && shopCarList?.data?.size != 0)
                            v.dismissLoading()
                        }
                    }

                    override fun onFail(error: ApiException) {
                        if (hasV()){
                            v.dismissLoading()
                        }
                    }
                })
    }
}